Skip to content

Add OpenMP kernels#389

Open
mcbarton wants to merge 21 commits intocompiler-research:mainfrom
mcbarton:Add-OpenMP-kernel
Open

Add OpenMP kernels#389
mcbarton wants to merge 21 commits intocompiler-research:mainfrom
mcbarton:Add-OpenMP-kernel

Conversation

@mcbarton
Copy link
Collaborator

@mcbarton mcbarton commented Oct 7, 2025

Description

Please include a summary of changes, motivation and context for this PR.

Fixes # (issue)

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Added/removed dependencies
  • Required documentation updates

@codecov-commenter
Copy link

codecov-commenter commented Oct 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.94%. Comparing base (c357fd6) to head (903e597).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #389   +/-   ##
=======================================
  Coverage   81.94%   81.94%           
=======================================
  Files          21       21           
  Lines         853      853           
  Branches       87       87           
=======================================
  Hits          699      699           
  Misses        154      154           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcbarton mcbarton force-pushed the Add-OpenMP-kernel branch 3 times, most recently from e4af2d6 to 3824a2f Compare October 9, 2025 07:23
@mcbarton
Copy link
Collaborator Author

mcbarton commented Oct 9, 2025

Things done so far

  • Add c++ + openmp kernels (includes kernel definition json files added, and cmake change)
  • Updated documentation so that LD_LIBRARY_PATH is defined
  • Add example c++ + openmp notebooks which have been tested locally to work (taken from xeus-clang-repl)

List of things still to do

  • Add c + openmp kernels
  • Update ci so LD_LIBRARY_PATH is defined and openmp kernels can be tested
  • Add some tests for the openmp kernels

@vgvassilev
Copy link
Contributor

We should probably have a openmp specific ci without having to rely on the ld_library_path..

@mcbarton
Copy link
Collaborator Author

mcbarton commented Oct 9, 2025

We should probably have a openmp specific ci without having to rely on the ld_library_path..

@vgvassilev Having an openmp specific ci will not avoid the need for ld_library_path. To avoid ld_library_path you would need to rework the cmake and the kernelspec (which makes reference to this variable) , and the example notebooks would need to know the path where you stored the openmp library. Using ld_library_path like xeus-clang-repl did simplifies this PR.

@mcbarton mcbarton force-pushed the Add-OpenMP-kernel branch 3 times, most recently from 45a4ee5 to 571023c Compare October 9, 2025 12:43
@vgvassilev
Copy link
Contributor

We should probably have a openmp specific ci without having to rely on the ld_library_path..

@vgvassilev Having an openmp specific ci will not avoid the need for ld_library_path. To avoid ld_library_path you would need to rework the cmake and the kernelspec (which makes reference to this variable) , and the example notebooks would need to know the path where you stored the openmp library. Using ld_library_path like xeus-clang-repl did simplifies this PR.

Okay, let's see if that's compatible with the vision of @SylvainCorlay which he expressed recently.

#include <omp.h>
#include <iostream>
#include <clang/Interpreter/CppInterOp.h>
Cpp::LoadLibrary("libomp");
Copy link
Collaborator Author

@mcbarton mcbarton Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp::LoadLibrary("libomp"); works in the notebooks, but doesn't appear to be working in the python kernel tests. I believe this because the tests fail with the same error that I get if I remove this line from the notebook (see https://github.com/compiler-research/xeus-cpp/actions/runs/18385849945/job/52383968040?pr=389#step:13:1015 for error). @vgvassilev @anutosh491 Any idea what may be going on?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only guess is that the kernelspecs have the flag -fopenmp=libomp , but the python output has the fopenmp flag without the =libomp (see https://github.com/compiler-research/xeus-cpp/actions/runs/18385849945/job/52383968040?pr=389#step:13:922 ). I don't know where to change it so that it has -fopenmp=libomp too though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call the Cpp::LoadLibrary("libomp") in a separate declare operation?

Copy link
Collaborator Author

@mcbarton mcbarton Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you was suggesting splitting this code into 2 executions, one which loads the library, and one which executes the openmp code, but that has worked, and we now have a passing test. Its a simple OpenMP test, but hopefully it will suffice.

@mcbarton mcbarton force-pushed the Add-OpenMP-kernel branch 2 times, most recently from 5d1ad18 to 5a2630d Compare October 10, 2025 07:47
@mcbarton
Copy link
Collaborator Author

mcbarton commented Oct 10, 2025

@vgvassilev @alexander-penev @Vipul-Cariappa @anutosh491 @SylvainCorlay I now consider this PR ready for review. It adds c and c++ kernels that have openmp. It includes example notebooks for the c++ + openmp kernels taken from xeus-clang-repl which were able to run when I tried locally. It also has one simple openmp test for the jupyter kernel test tests. I wasn't exactly sure what would suffice as an automatic test, but the test clearly demonstrates the kernel is executing OpenMP code.

Copy link
Collaborator

@Vipul-Cariappa Vipul-Cariappa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to these changes; The user after selecting a omp kernel, still needs to include CppInterOp.h and call Cpp::LoadLibrary(...). The user should not be expected to know the internals and do this.
When we launch a kernel that enables OpenMP, it should automatically load all necessary stuff. You need to parse the compiler arguments, check if it contains -fopenmp, if yes, call LoadLibrary.
Also, can you share the contents of kernel.json files that gets installed.

Stretch goal (may not be possible): Can we get it to work, without modifying the LD_LIBRARY_PATH env var?

@mcbarton
Copy link
Collaborator Author

mcbarton commented Oct 14, 2025

I think I have an idea of how to avoiding needing ld library path. My new method should work on the Windows once we have that platform working too. I just need to test it out, and plan to test it out sometime in the next few days.

I'm not so convinced that making the user have to include CppInterOp.h, and calling cpp::LoadLibrary(...) is as big deal as you make out to be. It would also let the user know to use other (non openmp) shared libraries in their notebooks. The example notebooks are there for a reason. I kept this PR the way xeus-clang-repl did openmp kernels. Despite this I will look into working out to have the kernel automatically do this if it notices -fopenmp in the kernel arguments.

@mcbarton mcbarton force-pushed the Add-OpenMP-kernel branch 3 times, most recently from 2dac7b4 to a41ed55 Compare October 15, 2025 12:34
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

// FIXME: We should process the kernel input options and conditionally pass
// the gpu args here.
return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/);
Cpp::TInterp_t res = Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "CppImpl::CreateInterpreter" is directly included [misc-include-cleaner]

  Cpp::TInterp_t res = Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
                            ^

// FIXME: We should process the kernel input options and conditionally pass
// the gpu args here.
return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/);
Cpp::TInterp_t res = Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "CppImpl::TInterp_t" is directly included [misc-include-cleaner]

  Cpp::TInterp_t res = Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
       ^

)
!= ClangArgs.end())
{
Cpp::LoadLibrary("libomp");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "CppImpl::LoadLibrary" is directly included [misc-include-cleaner]

      Cpp::LoadLibrary("libomp");
           ^

@AntoinePrv
Copy link

Should we take the changes to the existing kernels and the CMakeLists.txt changes not related to openMP in a separate PR and merge that?

I am interested in these changes but I see this PR is opened since october.

@mcbarton
Copy link
Collaborator Author

mcbarton commented Feb 3, 2026

Should we take the changes to the existing kernels and the CMakeLists.txt changes not related to openMP in a separate PR and merge that?

I am interested in these changes but I see this PR is opened since october.

@AntoinePrv This PR has been open for a while because certain issues needed to be resolved. I got the OpenMP kernels working, but it required a hack due to issue with CppInterOp (see this issue compiler-research/CppInterOp#784). The reason ci is currently red is because I prepared it for a new CppInterOp release which has a patch created by @Vipul-Cariappa in CppInterOp to workaround the issue.

cc @vgvassilev

set(XEUS_CPP_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}")
set(XEUS_CPP_INCLUDE_DIR ${CMAKE_INSTALL_PREFIX}/include)
if(${kernel} MATCHES "omp/$")
set(XEUS_CPP_OMP "${OpenMP_CXX_FLAGS}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this? Is that the include path to openmp? I feel uncomfortable exporting that information at build time whereas it's needed at runtime and we should be able to resolve that then. However, that's not a new problem per se because we already to this for other things..

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Consider squash on merge.

Copy link
Collaborator

@anutosh491 anutosh491 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be really honest, I'm not convinced with this.

Please go through this comment of mine : #432 (comment) (especially point 2)

Just the idea of enabling OpenMP by default is not sitting well with me

https://github.com/compiler-research/xeus-cpp/pull/389/files#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aR110

Sorry for my limited capacity to look into this today (as I'm Brussels after FOSDEM & shall reach back home in India tomorrow night) but commenting at a surface level I'll just say an openmp kernel looks like a specific use case to me. And changes to the environment file/CMakelists for finding relevant paths should only be done if the user ends up wanting to build an openmp kernel right ?

I wouldn't mind it if is was not bringing in changes to the "default"

For eg all wasm kernels are SIMD enabled (which technically is a specific use case)

"-mllvm", "-wasm-enable-sjlj", "-msimd128"

but it doesn't need any extra cmake changes whatsoever.

I would really like @JohanMabille or @SylvainCorlay to look at this before we take this in.

Also sorry again for missing any discussion done above as to how we proceed in the future but I see the CI is red too, so not sure we can merge.

If the parameterized JEP is the solution for handling this better: jupyter/enhancement-proposals#87
Let's just use it !

But if it's not and we if just go ahead with this and refactor later, I would like to atleast see an issue on github as to what the plan is !

@vgvassilev
Copy link
Contributor

vgvassilev commented Feb 4, 2026

I'll be really honest, I'm not convinced with this.

Please go through this comment of mine : #432 (comment) (especially point 2)

I share the same sentiment, however, there is practically no way to do this as of today. We have been waiting for a solution to this issue since 2021 and I do not think it is reasonable to drop major features like openmp for this reason unfortunately.

Just the idea of enabling OpenMP by default is not sitting well with me

https://github.com/compiler-research/xeus-cpp/pull/389/files#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aR110

Sorry for my limited capacity to look into this today (as I'm Brussels after FOSDEM & shall reach back home in India tomorrow night) but commenting at a surface level I'll just say an openmp kernel looks like a specific use case to me. And changes to the environment file/CMakelists for finding relevant paths should only be done if the user ends up wanting to build an openmp kernel right ?

I wouldn't mind it if is was not bringing in changes to the "default"

Blocking this would also block the conda recipe and other changes proposed by Antoine because I believe they will cause more friction when this work resumes/goes in. In fact, we are moving forward and had a few emergency meetings so that we don't have to block Antoine's work and the current state of the PR is good enough. I agree this cannot go forever but that's not a good argument to disallow omp kernels -- that's one of the best use cases to teach parallel programming with xeus-cpp.

For eg all wasm kernels are SIMD enabled (which technically is a specific use case)

"-mllvm", "-wasm-enable-sjlj", "-msimd128"

but it doesn't need any extra cmake changes whatsoever.

I would really like @JohanMabille or @SylvainCorlay to look at this before we take this in.

Also sorry again for missing any discussion done above as to how we proceed in the future but I see the CI is red too, so not sure we can merge.

If the parameterized JEP is the solution for handling this better: jupyter/enhancement-proposals#87 Let's just use it !

But if it's not and we if just go ahead with this and refactor later, I would like to atleast see an issue on github as to what the plan is !

I would be more than happy to drop all kernels when this becomes widely available. @mcbarton can you open an issue to track this?

Copy link
Collaborator

@anutosh491 anutosh491 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay then, let's track the development here through an issue and then let's take this in !

Thank you for the explanation :)

@JohanMabille
Copy link
Collaborator

JohanMabille commented Feb 4, 2026

It seems that we all agree that this solution is not ideal but we don't really have an alternative for now. Also this does not scale on the long run (supporting CUDA would reqiure 6 more kernels and additional dependencies for instance).

I have good hope that the parametrized kernelspec JEP will be merged soon and I'll tackle its implementation in xeus right after. I think we'll also need to add a plugin system so that the additional required code (for loading libraries / initializing components / whatever) can live out of the "core" kernel.

That being said, I don't think this PR is ready to merge because of the CI failures. The failing jobs should be fixed before we merge this.

Copy link
Collaborator

@Vipul-Cariappa Vipul-Cariappa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too believe the CMake changes may not be required. At least the way it is implemented now. But we can track it in an issue, and fix it later.
Reasoning: OpenMP is not used by xeus-cpp itself (at least at build time), it is just an optional runtime dependency to the interpreter instance. Nowhere in xeus-cpp code we have #include <omp.h> ...

Otherwise, LGTM!

@SylvainCorlay
Copy link
Collaborator

On the packaging end, we will probably split the conda recipe in multiple outputs (so that installing xeus-cpp does not include the OpenMP kernelspecs unless e.g. xeus-cpp-openmp is installed).

I would also like to highlight that the Jupyter Enhancement Proposal was specifically created to address the C++ kernel use case. It has undergone extensive reviews, and our team produced a reference implementation covering kernel, server, and frontend components. The JEP is currently in the final voting phase.

Given that the official standardization process is nearly complete, it is disappointing to rush a workaround while the long-term solution is so close to being finalized and was developed specifically with the C++ kernel in mind.

@vgvassilev
Copy link
Contributor

On the packaging end, we will probably split the conda recipe in multiple outputs (so that installing xeus-cpp does not include the OpenMP kernelspecs unless e.g. xeus-cpp-openmp is installed).

I would also like to highlight that the Jupyter Enhancement Proposal was specifically created to address the C++ kernel use case. It has undergone extensive reviews, and our team produced a reference implementation covering kernel, server, and frontend components. The JEP is currently in the final voting phase.

Given that the official standardization process is nearly complete, it is disappointing to rush a workaround while the long-term solution is so close to being finalized and was developed specifically with the C++ kernel in mind.

Do we have an ETA for this?

@mcbarton mcbarton force-pushed the Add-OpenMP-kernel branch 2 times, most recently from d17cf3e to 58eaaa7 Compare February 4, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants